<?php
class Project_User_Table extends CF_DbTable_Table{
	protected $_name = 'users';

	public function getPaginatorUsers ($pageNumber = 1, $sortFields = null, $sortSearchValue = null, $itemCountPerPage = 3)
	{
		$pAdapter = new Zend_Paginator_Adapter_DbSelect($this->_getUsers($sortFields, $sortSearchValue));
		$paginator = new Zend_Paginator($pAdapter);
		$paginator->setCurrentPageNumber($pageNumber);
		$paginator->setItemCountPerPage($itemCountPerPage);
		//Zend_Paginator::setDefaultScrollingStyle('Sliding');

		return $paginator;
	}

	private function _getUsers($sortFields = null, $sortSearchValue = null)
	{

		$query = $this->getAdapter()->select()
			->from(array('us'=>'users'),
				array('id','username','date_created'))
			->joinLeft(array('r'=>'roles'),
				'us.role_id=r.id',
				array('role'));

		if(!is_null($sortFields)){
			if(is_array($sortFields)){
				foreach($sortFields as $field){
					//$safeSortFields = $this->getAdapter()->quote($field);
					$query->order($field);
				}
			}else{
				$query->order($sortFields);
			}
		}
		if(!is_null($sortSearchValue))
		{
			$query->where('username LIKE ?',$sortSearchValue.'%');
		}
		//exit;
		//$this->_users_query = $query;
		return  $query;
	}

	public function getUser($id)
	{
		return  $this->fetchRow(
		$this->select()
		->where('id = ?',$id)
		);
	}
	public function getRoles($roleId = null){
		$query = $this->getAdapter()->select()
			->from('roles');
		if(!is_null($roleId)){
			$query->where('id=?',$roleId)
				->limit(1);
			$resource = $this->getAdapter()->query($query);
			$result = $resource->fetchAll();
			return $result[0];	
		}	
		$resource = $this->getAdapter()->query($query);
		$result = $resource->fetchAll();
		
		return $result;
	}
	public function addUser($username, $password, $role_id)
	{
		$salt = sha1($password);//let it salt be a sha1 of password
		$sha_password = sha1($password.$salt);
		$date = date("Y-m-d H:m:s");
		//echo $date;
		//exit; 
		$data = array(
            'username' => $username,
			'password' => $sha_password,
			'salt' => $salt,
			'role_id' => $role_id,
			'date_created' => $date
		);
		$this->insert($data);
	}
//(10, 'KAN', SHA1(CONCAT('KAN',SHA1(1))), SHA1(1), 'manager');
	/*
	 * @param array $userAttributes
	 * with key as table fields and value as value for them :) 
	 */
	public function updateUser(array $userAttributes)
	{
		$id = $userAttributes['id'];
		$data = array();
		foreach($userAttributes as $attr=>$val){
			switch ($attr){
				case 'username':
					$data[$attr] = $val;
					break;
				case 'password':
					$salt = sha1($val);
					$sha_password = sha1($val.$salt);
					$data[$attr] = $sha_password;
					$data['salt'] = $salt;
					break;
				case 'role_id'	:
					$data[$attr] = $val;
					break;
			}
		}

		$this->update($data, 'id = '.(int)$id);
	}

	public function deleteUser($id)
	{
		$this->delete('id =' . (int)$id);
	}
	
}
